package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopGoodListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopSearchBeen;
import cn.xfdzx.android.apps.shop.contract.GoodActionCallback;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * @author gzk
 * @date 2020/3/27 0027
 * description：
 */
public class ShopSearchViewHolder extends RecyclerView.ViewHolder {


//    @BindView(R.id.tv_goods_title_right)
//    TextView tvGoodsTitleRight;
    @BindView(R.id.tv_shop_goods_name)
    TextView tvShopGoodsName;
    @BindView(R.id.iv_shop_picture)
    ImageView ivShopPicture;
    @BindView(R.id.tv_shop_month_sale)
    TextView tvShopMonthSale;
    @BindView(R.id.tv_shop_high_praise)
    TextView tvShopHighPraise;
    @BindView(R.id.tv_shop_goods_price)
    TextView tvShopGoodsPrice;
    @BindView(R.id.tv_shop_goods_price_old)
    TextView tvShopGoodsPriceOld;
    @BindView(R.id.tv_goods_add)
    TextView tvGoodsAdd;
    @BindView(R.id.tv_goods_add_bg)
    TextView tvGoodsAddBg;
    @BindView(R.id.tv_goods_minus_bg)
    TextView tvGoodsMinusBg;
    @BindView(R.id.tv_goods_minus)
    TextView tvGoodsMinus;
    @BindView(R.id.tv_goods_num)
    TextView tvGoodsNum;


    private Context mContext;
    private GoodActionCallback callback;

    public ShopSearchViewHolder(Context mContext, View itemView, GoodActionCallback callback) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        this.mContext = mContext;
        this.callback = callback;
    }


    public void setData(ShopSearchBeen.DataBean goodsBeen, final int position) {

//        tvShopGoodsName.setText(goodsBeen.getTitle()+ (int)Double.parseDouble(goodsBeen.getGoods_weight())+"g");
        tvShopGoodsName.setText(goodsBeen.getTitle() );
        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(goodsBeen.getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivShopPicture);

        tvShopMonthSale.setText("月售"+goodsBeen.getSale_num());
//        tvShopHighPraise.setText(goodsBeen.highPraise);
        if (Double.parseDouble(goodsBeen.getDiscountprice()) == 0) {
            tvShopGoodsPrice.setText("¥"+goodsBeen.getMarketprice());
//            tvShopGoodsPriceOld.setText(goodsBeen.goodsPriceOld);
            tvShopGoodsPriceOld.setVisibility(View.INVISIBLE);
        } else {
            tvShopGoodsPriceOld.setVisibility(View.VISIBLE);
            tvShopGoodsPrice.setText("¥"+goodsBeen.getDiscountprice());
            tvShopGoodsPriceOld.setText("¥"+goodsBeen.getMarketprice());
        }
        tvShopGoodsPriceOld.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

        tvGoodsNum.setText(goodsBeen.getNum() + "");
        if (goodsBeen.getNum() == 0) {
            tvGoodsMinus.setVisibility(View.INVISIBLE);
            tvGoodsMinusBg.setVisibility(View.INVISIBLE);
            tvGoodsNum.setVisibility(View.INVISIBLE);
        } else {
            tvGoodsMinus.setVisibility(View.VISIBLE);
            tvGoodsMinusBg.setVisibility(View.VISIBLE);
            tvGoodsNum.setVisibility(View.VISIBLE);
        }
        tvGoodsAddBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (EmptyUtil.isEmpty(APP_TOKEN)){
                    Intent intent = new Intent(mContext, OneLoginMainActivity.class);
                    mContext.startActivity(intent);
                    return;
                }
//                tvGoodsMinus.setVisibility(View.VISIBLE);
//                tvGoodsNum.setVisibility(View.VISIBLE);
//                goodsBeen.setGood_sum(goodsBeen.getGood_sum()+1);
//                tvGoodsNum.setText(goodsBeen.getGood_sum() + "");
                callback.addAction(tvGoodsAdd, position, true);

            }
        });
        tvGoodsMinusBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (EmptyUtil.isEmpty(APP_TOKEN)){
                    Intent intent = new Intent(mContext, OneLoginMainActivity.class);
                    mContext.startActivity(intent);
                    return;
                }

                callback.minusGood(position,true);
//                goodsBeen.setGood_sum(goodsBeen.getGood_sum()-1);
//                if (goodsBeen.getGood_sum() == 0) {
//                    tvGoodsMinus.setVisibility(View.INVISIBLE);
//                    tvGoodsNum.setVisibility(View.INVISIBLE);
//                }
//                tvGoodsNum.setText(goodsBeen.getGood_sum() + "");
            }
        });
    }


}





